<%-- 
    Document   : db1
    Created on : 2014/8/19, 下午 01:28:56
    Author     : gjun
--%>

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    //import時 注意要java.sql.的開頭
    Class.forName("com.mysql.jdbc.Driver");//載入資料庫驅動程式
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javadb","root","root") ;
          //  .getConnection(Services頁-內的database的connection內的URL,帳號,密碼)
    Statement st= con.createStatement();
    String sql="select id,username,tel,birthday from person";
          //把sql的新增語法從Navicat 的新增查詢 測試過OK的加入
    
    System.out.println(sql);
       
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>select table</h1>
        <table border="1" >
            
        
                
        <% 
            ResultSet rs = st.executeQuery(sql); 
            while(rs.next()){//next有下一筆為ture
                String id=rs.getString("id");
                String username=rs.getString("username");
                String tel=rs.getString("tel");
                String birthday=rs.getString("birthday");
                //out.println(id+","+username+","+tel+","+birthday+"<br>");
        %>     
             <tr>
                <td><%= id %></td>
                <td><%= username%></td>
                <td><%= tel%></td>
                <td><%= birthday%></td>
                <td><a href="update1.jsp?id=<%= id %>">更新</a>
                <input type="button" value="Delete" onclick="doDelete('<%= id %>')"></td>
            </tr>   
        <%
                }
            rs.close();
            st.close();
            con.close();
        
        %>
        </table>
        <script>
            //從onclick 呼叫Javascript--> function doDelete()
            function doDelete(x){//x是接onclick="doDelete(' ....')" 的資料
//            alert('x(id)'+x);//從onclick 呼叫Javascript--> function doDelete()
                if(confirm('確認刪除?') ){
                    //alert('a');
                    document.getElementById('id').value= x;
                    //alert(x);
                    document.getElementById('form1').submit();
                }
            }
            
        </script>
        <form　action="delete.jsp" id="form1">
            <input type="hidden"  name="id" id="id" value="">
        </form>
    </body>
</html>
